package com.hxkcloud.patient.popup;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;


public class PictureSelectPopupWindow extends PopupWindow implements
		OnClickListener {

	private FinishListener finishListener;
	private Context context;

	private RelativeLayout mSelectView;

	private TextView select_camera;
	private TextView select_gallery;
	private TextView select_cancel;

	private int screenWidth;
	private int screenHeight;
	private int type = 1;//打开类型：1 = 图片（只能拍摄或选择图片），2 = 视频（只能拍摄或选择视频）,默认只打开图片

	public PictureSelectPopupWindow(Context context,int screenWidth, int screenHeight,
			FinishListener finishListener) {
		this.context = context;
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		this.finishListener = finishListener;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mSelectView = (RelativeLayout) inflater.inflate(
				R.layout.popup_picture_select, null);

		select_camera = (TextView) mSelectView.findViewById(R.id.select_camera);
		select_gallery = (TextView) mSelectView.findViewById(R.id.select_gallery);
		select_cancel = (TextView) mSelectView.findViewById(R.id.select_cancel);

		select_camera.setOnClickListener(this);
		select_gallery.setOnClickListener(this);
		select_cancel.setOnClickListener(this);

		// 设置mSelectView的View
		this.setContentView(mSelectView);
		// 设置mSelectView弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置mSelectView弹出窗体的高
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置mSelectView弹出窗体可点击
		this.setFocusable(true);
//		// 实例化一个ColorDrawable颜色为半透明
//		ColorDrawable dw = new ColorDrawable(context.getResources().getColor(android.R.color.transparent));
//		// 设置mSelectView弹出窗体的背景
//		this.setBackgroundDrawable(dw);
		// mSelectView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mSelectView.setOnTouchListener(new OnTouchListener() {

			@SuppressLint("ClickableViewAccessibility")
			public boolean onTouch(View v, MotionEvent event) {

				int height = mSelectView.findViewById(R.id.popup_window_select)
						.getTop();
				int y = (int) event.getY();

				if (event.getAction() == MotionEvent.ACTION_UP) {

					if (y < height) {

						dismiss();

					}
				}
				return true;
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.select_camera:
			finishListener.onFinished(1, type);
			dismiss();
			break;
		case R.id.select_gallery:
			finishListener.onFinished(2, type);
			dismiss();
			break;
		case R.id.select_cancel:
			dismiss();
			break;
		}
	}

	public void setType(int type){
		this.type = type;
	}

	public interface FinishListener {
		void onFinished(int options, int type);
	}

}
